/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  ZoneSearchNullFragment.java   
 * @Package com.jiyi.activity.zone   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-13 下午4:54:45   
 * @version V1.0     
 */
package com.jiyi.activity.zone;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hyphenate.easeui.EaseConstant;
import com.jiyi.bean.zone.HuoDongDetailJson;
import com.jiyi.bean.zone.QunZuDetailJson;
import com.jiyi.bean.zone.ZoneHaoYouBean;
import com.jiyi.bean.zone.ZoneHuoDongBean;
import com.jiyi.bean.zone.ZoneQunZuBean;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.ClouldChatType;
import com.jiyi.easemob.view.EasemobChatActivity;
import com.jiyi.inteface.HuoDongPopInterface;
import com.jiyi.inteface.QunZuPopInterface;
import com.jiyi.inteface.WebApi;
import com.jiyi.view.HuoDongPopView;
import com.jiyi.view.QunZuPopView;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;
import com.swift.maintenance.utils.ZoneHuoDongUtil;
import com.swift.utils.DateUtils;
import com.swift.utils.GlideUtil;
import com.swift.widget.TranslucentImageView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ClassName: ZoneSearchNullFragment
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-6-13 下午4:54:45
 * 
 */
public class ZoneSearchAllFragment extends Fragment implements
		QunZuPopInterface,HuoDongPopInterface {

	@BindView(R.id.llHaoYou)
	LinearLayout llHaoYou;
	@BindView(R.id.llQunZu)
	LinearLayout llQunZu;
	@BindView(R.id.llHuoDong)
	LinearLayout llHuoDong;

	@BindView(R.id.llHaoYouDatas)
	LinearLayout llHaoYouDatas;
	@BindView(R.id.llQunZuDatas)
	LinearLayout llQunZuDatas;
	@BindView(R.id.llHuoDongDatas)
	LinearLayout llHuoDongDatas;
	@BindView(R.id.svData)
	ScrollView svData;
	
	
	//空提示
	@BindView(R.id.tvTip)
	TextView tvTip;

	List<ZoneHaoYouBean> haoYouDatas = new ArrayList<ZoneHaoYouBean>();
	List<ZoneQunZuBean> tuiJianQunZuDatas = new ArrayList<ZoneQunZuBean>();
	List<ZoneHuoDongBean> huoDongDatas = new ArrayList<ZoneHuoDongBean>();
	QunZuPopView mQunZuPopView;
	HuoDongPopView mHuoDongPopView;
	//搜索类别
	int type = 0;

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_zone_search_all, null,
				false);
		ButterKnife.bind(this, view);
		init();
		return view;
	}

	/**
	 * @author: HYM
	 * @Title: init
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-6-14
	 */
	private void init() {
		getIntentData();
		switch (type) {
		case 0:
			if ((haoYouDatas == null || haoYouDatas.size() <= 0)
					&& (tuiJianQunZuDatas == null || tuiJianQunZuDatas.size() <= 0)
					&& (huoDongDatas == null || huoDongDatas.size() <= 0)) {
				svData.setVisibility(View.GONE);
				tvTip.setVisibility(View.VISIBLE);
			} else {
				svData.setVisibility(View.VISIBLE);
				tvTip.setVisibility(View.GONE);
			}

			// 全部搜索
			if (haoYouDatas != null && haoYouDatas.size() > 0) {
				fillHaoYouData();
			} else {
				llHaoYou.setVisibility(View.GONE);
			}
			if (tuiJianQunZuDatas != null && tuiJianQunZuDatas.size() > 0) {
				fillTuiJianQunZuData();
			} else {
				llQunZu.setVisibility(View.GONE);
			}
			if (huoDongDatas != null && huoDongDatas.size() > 0) {
				fillHuoDongData();
			} else {
				llHuoDong.setVisibility(View.GONE);
			}

			break;
		case 1:
			if ((huoDongDatas == null || huoDongDatas.size() <= 0)) {
				svData.setVisibility(View.GONE);
				tvTip.setVisibility(View.VISIBLE);
			} else {
				tvTip.setVisibility(View.GONE);
				svData.setVisibility(View.VISIBLE);
			}
			// 搜索活动
			llHaoYou.setVisibility(View.GONE);
			llQunZu.setVisibility(View.GONE);
			if (huoDongDatas != null) {
				fillHuoDongData();
			}
			break;
		case 2:
			if ((tuiJianQunZuDatas == null || tuiJianQunZuDatas.size() <= 0)) {
				svData.setVisibility(View.GONE);
				tvTip.setVisibility(View.VISIBLE);
			} else {
				tvTip.setVisibility(View.GONE);
				svData.setVisibility(View.VISIBLE);
			}
			// 搜索群组
			llHaoYou.setVisibility(View.GONE);
			llHuoDong.setVisibility(View.GONE);
			if (tuiJianQunZuDatas != null) {
				fillTuiJianQunZuData();
			}
			break;
		case 3:
			if ((haoYouDatas == null || haoYouDatas.size() <= 0)) {
				svData.setVisibility(View.GONE);
				tvTip.setVisibility(View.VISIBLE);
			} else {
				tvTip.setVisibility(View.GONE);
				svData.setVisibility(View.VISIBLE);
			}
			// 搜索好友
			llQunZu.setVisibility(View.GONE);
			llHuoDong.setVisibility(View.GONE);
			if (haoYouDatas != null) {
				fillHaoYouData();
			}
			break;
		default:
			break;
		}

		mQunZuPopView = new QunZuPopView(getActivity(), this);
		mHuoDongPopView = new HuoDongPopView(getActivity(), this);
	}

	/**
	 * @author: HYM
	 * @Title: getIntentData
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-6-14
	 */
	private void getIntentData() {
		type = getArguments().getInt("type");
		String haoYouJson = getArguments().getString("haoyou");
		haoYouDatas = new Gson().fromJson(haoYouJson,
				new TypeToken<List<ZoneHaoYouBean>>() {
				}.getType());
		String qunZuJson = getArguments().getString("qunzu");
		tuiJianQunZuDatas = new Gson().fromJson(qunZuJson,
				new TypeToken<List<ZoneQunZuBean>>() {
				}.getType());
		String huoDongJson = getArguments().getString("huodong");
		huoDongDatas = new Gson().fromJson(huoDongJson,
				new TypeToken<List<ZoneHuoDongBean>>() {
				}.getType());
	}

	void fillHaoYouData() {
		llHaoYouDatas.removeAllViews();
		for (int i = 0; i < haoYouDatas.size(); i++) {
			final ZoneHaoYouBean obj = haoYouDatas.get(i);
			View view = LayoutInflater.from(getContext()).inflate(
					R.layout.item_zone_haoyou_notalk, null, false);
			RelativeLayout rlParent = (RelativeLayout) view
					.findViewById(R.id.rlParent);
			rlParent.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(v.getContext(),
							PersonInfoActivity.class);
					intent.putExtra("id", obj.getId());
					startActivity(intent);
				}
			});
			TextView tvName = (TextView) view.findViewById(R.id.tvName);
			tvName.setText(obj.getName());
			ImageView ivLogo = (ImageView) view.findViewById(R.id.ivLogo);
			GlideUtil.load(getActivity(), obj.getHead_path(), ivLogo);
			llHaoYouDatas.addView(view);
			View spi = new View(getContext());
			spi.setBackgroundColor(getResources().getColor(R.color.spi_grey));
			LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, 1);
			spi.setLayoutParams(params);
			llHaoYouDatas.addView(spi);
		}
	}

	void fillTuiJianQunZuData() {
		llQunZuDatas.removeAllViews();
		for (int i = 0; i < tuiJianQunZuDatas.size(); i++) {
			ZoneQunZuBean obj = tuiJianQunZuDatas.get(i);
			View view = LayoutInflater.from(getContext()).inflate(
					R.layout.item_zone_qunzu, null, false);
			RelativeLayout rlRoot = (RelativeLayout) view
					.findViewById(R.id.rlRoot);
			TextView tvQunZuName = (TextView) view
					.findViewById(R.id.tvQunZuName);
			tvQunZuName.setText(obj.getName());
			ImageView ivLogo = (ImageView) view.findViewById(R.id.ivLogo);
			GlideUtil.load(getActivity(), obj.getGroup_head(), ivLogo);
			TextView tvJoinNum = (TextView) view.findViewById(R.id.tvJoinNum);
			tvJoinNum.setText(obj.getMember()+"人");
			TextView tvHuoDongNum = (TextView) view.findViewById(R.id.tvHuoDongNum);
			tvHuoDongNum.setText("发起活动："+obj.getActNum()+"次");
			rlRoot.setTag(obj);
			rlRoot.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					ZoneQunZuBean ooo = (ZoneQunZuBean) v.getTag();
					if (ooo.getIs_in_group()) {
						Intent intent2 = new Intent(getActivity(),
								EasemobChatActivity.class);
						intent2.putExtra(ClouldChatType.INTENT_USER_PIC,
								ooo.getGroup_head());
						intent2.putExtra("id", ooo.getId());
						intent2.putExtra("name", ooo.getName());
						intent2.putExtra("easemobgid", ooo.getHx_group_id());
						intent2.putExtra(ClouldChatType.INTENT_Group_Id,
								ooo.getId());
						intent2.putExtra("easeMobChatType",
								EaseConstant.CHATTYPE_GROUP);
						startActivity(intent2);
					} else {
						getQunZuDetail(ooo.getId());
					}
				}
			});
			view.findViewById(R.id.tvDate).setVisibility(View.INVISIBLE);
			llQunZuDatas.addView(view);
			View spi = new View(getContext());
			spi.setBackgroundColor(getResources().getColor(R.color.spi_grey));
			LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, 1);
			spi.setLayoutParams(params);
			llQunZuDatas.addView(spi);
		}
	}

	void fillHuoDongData() {
		llHuoDongDatas.removeAllViews();
		for (int i = 0; i < huoDongDatas.size(); i++) {
			final ZoneHuoDongBean obj = huoDongDatas.get(i);
			View view = LayoutInflater.from(getContext()).inflate(
					R.layout.item_zone_huodong, null, false);
			
			LinearLayout llRoot = (LinearLayout) view.findViewById(R.id.llRoot);
			TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
			tvTitle.setText(obj.getTheme());
			TranslucentImageView ivHuoDongLogo = (TranslucentImageView) view
					.findViewById(R.id.ivHuoDongLogo);
			GlideUtil.load(getActivity(), obj.getCover(), ivHuoDongLogo);
			ivHuoDongLogo.setTextString(obj.getAttendPer() + "人参与");
			TextView tvStartDate = (TextView) view
					.findViewById(R.id.tvStartDate);
			tvStartDate.setText(DateUtils.fmtDateToStr(obj.getStart_time(), DateUtils.YMD_FORMAT));
			TextView tvEndDate = (TextView) view.findViewById(R.id.tvEndDate);
			tvEndDate.setText(DateUtils.fmtDateToStr(obj.getEnd_time(), DateUtils.YMD_FORMAT));
			TextView tvAddress = (TextView) view.findViewById(R.id.tvAddress);
			tvAddress.setText(obj.getAddress());
			TextView tvJoin = (TextView) view.findViewById(R.id.tvJoin);
			int status = obj.getFlag();
			ZoneHuoDongUtil.setStatus(tvJoin, status);
			
			llRoot.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					getHuoDongDetail(obj.getId());
				}
			});
			
			llHuoDongDatas.addView(view);
			View spi = new View(getContext());
			spi.setBackgroundColor(getResources().getColor(R.color.spi_grey));
			LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, 1);
			spi.setLayoutParams(params);
			llHuoDongDatas.addView(spi);
		}
	}

	/**
	 * <p>
	 * Title: OnQZSureListener
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @see QunZuPopInterface#OnQZSureListener()
	 */
	@Override
	public void OnQZSureListener() {
		// TODO Auto-generated method stub

	}

	/**
	 * <p>
	 * Title: OnQZCancleListener
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @see QunZuPopInterface#OnQZCancleListener()
	 */
	@Override
	public void OnQZCancleListener() {
		// TODO Auto-generated method stub

	}

	/**
	 * <p>
	 * Title: OnQZShowListener
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @see QunZuPopInterface#OnQZShowListener()
	 */
	@Override
	public void OnQZShowListener() {
		// TODO Auto-generated method stub

	}

	/**
	 * <p>
	 * Title: OnQZHideListener
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @see QunZuPopInterface#OnQZHideListener()
	 */
	@Override
	public void OnQZHideListener() {
		// TODO Auto-generated method stub

	}

	/**
	 * @author: HYM
	 * @Title: getHuoDongDetail
	 * @Description: TODO(获取活动详情)
	 * @param qunZuId
	 *            void
	 * @date: 2017-7-17
	 */
	void getQunZuDetail(String qunZuId) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", qunZuId);

		VolleyRequest.StringRequestPost(getActivity(),
				WebApi.ZONE_QUNZU_DETAIL, "", params, new StringInterface(
						getActivity()) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								QunZuDetailJson temp = new Gson().fromJson(
										result, QunZuDetailJson.class);
								mQunZuPopView.showPopWindow(temp.getData());
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
						}
					}

					@Override
					public void stringError(VolleyError error) {
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
					}
				});
	}
	
	void getHuoDongDetail(String huoDongId)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", huoDongId);

		VolleyRequest.StringRequestPost(getActivity(), WebApi.ZONE_HUODONG_DETAIL, "",
				params, new StringInterface(getActivity()) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								HuoDongDetailJson temp = new Gson().fromJson(
										result, HuoDongDetailJson.class);
								mHuoDongPopView.showPopWindow(temp.getData());
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
						}
					}

					@Override
					public void stringError(VolleyError error) {
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
					}
				});
	}

	@Override
	public void OnSureListener() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void OnCancleListener() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void OnShowListener() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void OnHideListener() {
		// TODO Auto-generated method stub
		
	}
}
